// pages/home/home.js
//获取应用实例
var App = getApp();
var bmap = require('../../utils/bmap-wx.min.js');
var wxMarkerData = [];
var count = 10;
var total = 0;
var code = "2";
Page({
  data: {
    // 封面图片
    imageURL: [
      'https://7975-yunkaifa-5g55a0zx25478734-1305059131.tcb.qcloud.la/%E7%82%B9%E9%A4%90%E5%9B%BE%E7%89%87/cover/huoguo2.jpg?sign=bca32bbc9e6b2e6af4b3bce36e0959c6&t=1614929624',
      'https://7975-yunkaifa-5g55a0zx25478734-1305059131.tcb.qcloud.la/%E7%82%B9%E9%A4%90%E5%9B%BE%E7%89%87/cover/xican1.jpg?sign=76601f5c5bc0b5993fd29732387923b9&t=1614929749',
      'https://7975-yunkaifa-5g55a0zx25478734-1305059131.tcb.qcloud.la/%E7%82%B9%E9%A4%90%E5%9B%BE%E7%89%87/cover/zhongcan1.jpg?sign=dc72044751605872b8c595a8b46a4696&t=1614929763',
      'https://7975-yunkaifa-5g55a0zx25478734-1305059131.tcb.qcloud.la/%E7%82%B9%E9%A4%90%E5%9B%BE%E7%89%87/cover/zizhucan1.jpg?sign=0f79031162ca81cebb8f24dbce9e1185&t=1614929917',
      'https://7975-yunkaifa-5g55a0zx25478734-1305059131.tcb.qcloud.la/%E7%82%B9%E9%A4%90%E5%9B%BE%E7%89%87/cover/xiaochi1.jpg?sign=70f959f27682dcd48fab2aaf2368f26c&t=1614929783',
      'https://7975-yunkaifa-5g55a0zx25478734-1305059131.tcb.qcloud.la/%E7%82%B9%E9%A4%90%E5%9B%BE%E7%89%87/cover/shuiguo1.jpg?sign=94aeef35ed6f8dfe546d948587b7c67d&t=1614929801',
      'https://7975-yunkaifa-5g55a0zx25478734-1305059131.tcb.qcloud.la/%E7%82%B9%E9%A4%90%E5%9B%BE%E7%89%87/cover/miantiao1.jpg?sign=3e9a9fc80056e53651eb2834d0dce5f7&t=1614929815',
      'https://7975-yunkaifa-5g55a0zx25478734-1305059131.tcb.qcloud.la/%E7%82%B9%E9%A4%90%E5%9B%BE%E7%89%87/cover/hanbao2.jpg?sign=1fafdce4b0c8088b71b17328ec68416c&t=1614929851',
      'https://7975-yunkaifa-5g55a0zx25478734-1305059131.tcb.qcloud.la/%E7%82%B9%E9%A4%90%E5%9B%BE%E7%89%87/cover/zhongcan1.jpg?sign=06fd2f6eeff08853ace3c309584e1766&t=1614930043',
      'https://7975-yunkaifa-5g55a0zx25478734-1305059131.tcb.qcloud.la/%E7%82%B9%E9%A4%90%E5%9B%BE%E7%89%87/cover/hanbao2.jpg?sign=bb9d51e294cba7e5ae7da0bbb771d0e2&t=1614930069'
    ],
    value: "",
    aslect: false,
    bslect: true,
    cslect: true,
    dslect: true,
    eslect: true,
    title: "推荐",
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    markers: [],
    latitude: '',
    longitude: '',
    rgcData: {},
    telephone: {},
    test: '',
    interval: 3000,
    duration: 1000,
    array: [{
      code: '1',
      id: 'icon_1',
      src: 'https://7975-yunkaifa-5g55a0zx25478734-1305059131.tcb.qcloud.la/%E7%82%B9%E9%A4%90%E5%9B%BE%E7%89%87/%E7%81%AB%E9%94%85.png?sign=d98ad8712b207e1d7a1e880a62b73122&t=1614830340',
      text: '火锅'
    }, {
      code: '2',
      id: 'icon_2',
      src: 'https://7975-yunkaifa-5g55a0zx25478734-1305059131.tcb.qcloud.la/%E7%82%B9%E9%A4%90%E5%9B%BE%E7%89%87/%E8%A5%BF%E9%A4%90-1.png?sign=aa50de1ee9b0232ed76c69775498ae03&t=1614830358',
      text: '西餐'
    }, {
      code: '3',
      id: 'icon_3',
      src: 'https://7975-yunkaifa-5g55a0zx25478734-1305059131.tcb.qcloud.la/%E7%82%B9%E9%A4%90%E5%9B%BE%E7%89%87/%E4%B8%AD%E9%A4%90.png?sign=7be53a31902da42d003aa3f2e4b8ceed&t=1614830378',
      text: '中餐'
    }, {
      code: '4',
      id: 'icon_4',
      src: 'https://7975-yunkaifa-5g55a0zx25478734-1305059131.tcb.qcloud.la/%E7%82%B9%E9%A4%90%E5%9B%BE%E7%89%87/%E8%87%AA%E5%8A%A9%E9%A4%90.png?sign=c8c3ce45f6dadd742e7de0bf8ae54432&t=1614836481',
      text: '自助餐'
    }, {
      code: '5',
      id: 'icon_5',
      src: 'https://7975-yunkaifa-5g55a0zx25478734-1305059131.tcb.qcloud.la/%E7%82%B9%E9%A4%90%E5%9B%BE%E7%89%87/%E5%B0%8F%E5%90%83.png?sign=717645dd003dcf42813a06987e818008&t=1614836519',
      text: '小吃'
    }, {
      code: '6',
      id: 'icon_6',
      src: 'https://7975-yunkaifa-5g55a0zx25478734-1305059131.tcb.qcloud.la/%E7%82%B9%E9%A4%90%E5%9B%BE%E7%89%87/%E7%94%9F%E9%B2%9C-%E6%B0%B4%E6%9E%9C1.png?sign=67ff7d9793e765e87900beba4b2dc45c&t=1614836547',
      text: '水果'
    }, {
      code: '7',
      id: 'icon_7',
      src: 'https://7975-yunkaifa-5g55a0zx25478734-1305059131.tcb.qcloud.la/%E7%82%B9%E9%A4%90%E5%9B%BE%E7%89%87/%E9%9D%A2%E6%9D%A1.png?sign=dc30850f9b0ee38ae6dc841a569e5918&t=1614838660',
      text: '面食'
    }, {
      code: '8',
      id: 'icon_8',
      src: 'https://7975-yunkaifa-5g55a0zx25478734-1305059131.tcb.qcloud.la/%E7%82%B9%E9%A4%90%E5%9B%BE%E7%89%87/%E5%BF%AB%E9%A4%90.png?sign=c6f263d4ee57c1805af31e42da96859c&t=1614836597',
      text: '快餐'
    }],
    dataArray: []
  },
  //分类存储
  makertap: function(e) {
    var that = this;
    var id = e.markerId;
    that.showSearchInfo(wxMarkerData, id);
    that.setData({
      index_id: e.markerId,
    })
    console.log(e)
    this.showSearchInfo(e);
  },
  openPage: function (a) {
    var e = a.currentTarget.dataset.url;
    console.log(e)
    wx.reLaunch({
      url: e,
    })
  },
  onLoad: function(options) {
    var that = this;
    if (options.scene) {
      console.log("has scene");
      var scene = decodeURIComponent(options.scene);
      console.log("scene is ", scene);
      var arrPara = scene.split("&");
      var arr = [];
      for (var i in arrPara) {
        arr = arrPara[i].split("=");
        wx.setStorageSync(arr[0], arr[1]);
        console.log("setStorageSync:", arr[0], "=", arr[1]);
      }
    } else {
      console.log("no scene");
    }

    // 新建百度地图对象 
    var BMap = new bmap.BMapWX({
      ak: 'EjoZmUFpXh9LpkdGtNYH4a96i1sWYr08'
    });
    var fail = function(data) {
      console.log(data)
    };
    var success = function(data) {
      console.log(data);
      wxMarkerData = data.wxMarkerData;
      that.setData({
        markers: wxMarkerData,
        latitude: wxMarkerData[0].latitude,
        longitude: wxMarkerData[0].longitude,
        dataArray: data.wxMarkerData
      });
    };

    // 发起POI检索请求 
    BMap.search({
      "query": "美食",
      fail: fail,
      success: success,
    })
  },

  //搜索
  wxSearchFn: function(e) {
    console.log(e);
    if(e.detail === "") {
      return;
    }
    var that = this;
    total = 0;
    code = e.currentTarget.dataset.code + "";
    if (e.type === 'search') {
       var name = e.detail + "";
    } else {
      var name =  e.currentTarget.dataset.text + ""
    }
    this.data.dataArray = [];
    //显示选择结果
    this.setData({
      title: name
    })

    // 新建百度地图对象 
    var BMap = new bmap.BMapWX({
      ak: 'EjoZmUFpXh9LpkdGtNYH4a96i1sWYr08'
    });

    var fail = function(data) {
      console.log(data)
    };
    var success = function(data) {
      console.log(data);
      wxMarkerData = data.wxMarkerData;
      that.setData({
        markers: wxMarkerData,
        latitude: wxMarkerData[0].latitude,
        longitude: wxMarkerData[0].longitude,
        dataArray: data.wxMarkerData
      });
    };
    // 发起POI检索请求 
    BMap.search({
      query: name,
      fail: fail,
      success: success,
    });
  },

  showSearchInfo: function(data, i) {
    console.log("------------", data[i])
    var that = this;
    var Otitle = data[i].title;
    var Oaddress = data[i].address;
    wx.getLocation({//获取当前经纬度
      type: 'wgs84', //返回可以用于wx.openLocation的经纬度，官方提示bug: iOS 6.3.30 type 参数不生效，只会返回 wgs84 类型的坐标信息  
      success: function (res) {
        wx.openLocation({//​使用微信内置地图查看位置。
          latitude: data[i].latitude,//要去的纬度-地址
          longitude: data[i].longitude,//要去的经度-地址
          name: Otitle,
          address: Oaddress
        })
      }
    })
  },
  copyText: function (e) {
    var that = this,info_detail = e.currentTarget.dataset;
    wx.getLocation({//获取当前经纬度
      type: 'wgs84', //返回可以用于wx.openLocation的经纬度，官方提示bug: iOS 6.3.30 type 参数不生效，只会返回 wgs84 类型的坐标信息  
      success: function (res) {
        wx.openLocation({//​使用微信内置地图查看位置。
          latitude: info_detail.latitude,//要去的纬度-地址
          longitude: info_detail.longitude,//要去的经度-地址
          name: info_detail.title,
          address: info_detail.text
        })
      }
    })
  },
  onShareAppMessage: function (res) {
    //console.log("用户点击了确定", res)
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '美丽生活帮-看看周围有什么',
      path: 'pages/index/index',
      success: function (res) {
        // 转发成功
        
        wx.showShareMenu({
          // 要求小程序返回分享目标信息
          withShareTicket: true
        });
      },
      fail: function (res) {
        // 转发失败
        console.log("用户点击了取消",res)
      }
    }
  },
  toDetail(event) {
    let detail = event.currentTarget.dataset.item
    console.log('获取到对应的数据', event); 
    wx.navigateTo({
      url: '/pages/index/index?id=' + detail.id +'&title=' + detail.title + '&longitude=' + detail.longitude + '&latitude='+ detail.latitude +'&address='+ detail.address + '&telephone=' + detail.telephone,
    })
  },
  onReachBottom() {
   wx.showToast({
     icon: 'none',
     title: '已经拉到底了，尽情期待~',
   })
  }
})
